import 'package:flutter/widgets.dart';
import 'package:fluwx/fluwx.dart';
import 'package:sharesdk_plugin/sharesdk_defines.dart';
import 'package:sharesdk_plugin/sharesdk_interface.dart';
import 'package:sharesdk_plugin/sharesdk_map.dart';
import 'package:sharesdk_plugin/sharesdk_register.dart';
import 'package:tender_flutter/constant/app_config.dart';
import 'package:tender_flutter/constant/app_constant.dart';
import 'package:tender_flutter/constant/resource_path.dart';

typedef void ShareFinishCallback(bool success, String msg);

enum SharePlatform {
  wechcat,
  wechatLine,
  qq,
}

class ShareSdkUtils {
  /// 注册分享平台
  static registerSharePlatform() {
    ShareSDKRegister register = ShareSDKRegister();
    register.setupWechat(
        AppConfig.wx_key, AppConfig.wx_secret, AppConfig.wx_universalLink);
    register.setupQQ(AppConfig.qq_appid, AppConfig.qq_appKey);
    SharesdkPlugin.regist(register);
  }

  /// 分享内容
  static share(SharePlatform platform,
      {String title = AppConstant.SHARE_TITLE,
      String content = AppConstant.SHARE_CONTENT,
      String url,
      dynamic image,
      ShareFinishCallback callback}) {
    dynamic thumbnail =
        WeChatImage.asset(RPath.imgPath + "icon_big_shape_logo.png");

    if (platform == SharePlatform.wechcat) {
      if (image != null) {
        shareToWeChat(WeChatShareImageModel(image, scene: WeChatScene.SESSION));
      } else {
        shareToWeChat(WeChatShareWebPageModel(url,
            title: title,
            description: content,
            thumbnail: thumbnail,
            scene: WeChatScene.SESSION));
      }
    } else if (platform == SharePlatform.wechatLine) {
      if (image != null) {
        shareToWeChat(
            WeChatShareImageModel(image, scene: WeChatScene.TIMELINE));
      } else {
        shareToWeChat(WeChatShareWebPageModel(url,
            title: title,
            description: content,
            thumbnail: thumbnail,
            scene: WeChatScene.TIMELINE));
      }
    } else {
      SSDKMap params = SSDKMap()
        ..setGeneral(
          title,
          content,
          image,
          null,
          null,
          url,
          null,
          null,
          null,
          null,
          SSDKContentTypes.auto,
        );
      dynamic pla;
      switch (platform) {
        case SharePlatform.wechatLine:
          pla = ShareSDKPlatforms.wechatTimeline;
          break;
        case SharePlatform.wechcat:
          pla = ShareSDKPlatforms.wechatSession;
          break;
        case SharePlatform.qq:
          pla = ShareSDKPlatforms.qq;
          break;
      }
      SharesdkPlugin.share(pla, params, (SSDKResponseState state, Map userdata,
          Map contentEntity, SSDKError error) {
        debugPrint("分享结果${state}");
        debugPrint("分享结果${contentEntity}");
        debugPrint("分享结果${error.code}${error.rawData}");
        if (callback != null) {
          if (state == SSDKResponseState.Success) {
            callback(true, error.toString());
          } else {
            callback(false, error.toString());
          }
        }
      });
    }
  }
}
